package com.hn658.user.sync.user;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hn658.framework.shared.utils.JsonMapper;
import com.hn658.user.sync.Env;
import com.hn658.user.sync.auth.AuthHelper;
import com.hn658.user.sync.exception.OApiException;
import com.hn658.user.sync.httpClient.HttpHelper;
import com.hn658.user.sync.httpClient.HttpRequest;
import com.hn658.user.sync.httpClient.HttpResponse;

public class EmployeeHelper {
	private static JsonMapper mapper = JsonMapper.nonEmptyMapper();
	//创建成员
	public static String createUser(Employee user) throws OApiException {
		String accessToken = AuthHelper.getAccessToken();
		HttpRequest httpRequest = new HttpRequest(null);
		String url = Env.OAPI_HOST + "/user/create?" +
				"access_token=" + accessToken;
		httpRequest.setUrl(url);
		httpRequest.setMethod(HttpRequest.METHOD_POST);
		HttpResponse httpPost = HttpHelper.httpPost(httpRequest, user);
		String stringResult = httpPost.getStringResult();
		EmployeeResponse response = mapper.fromJson(stringResult,EmployeeResponse.class);
		if(response.getErrcode()!=0){
			throw new OApiException(String.valueOf(response.getErrcode()), response.getErrmsg(), new String[]{});
		}
		return response.getUserid();
	}
	
	
	//更新成员
	public static EmployeeResponse updateUser(Employee user) throws OApiException {
		String accessToken = AuthHelper.getAccessToken();
		HttpRequest httpRequest = new HttpRequest(null);
		String url = Env.OAPI_HOST + "/user/update?" +
				"access_token=" + accessToken;
		httpRequest.setUrl(url);
		httpRequest.setMethod(HttpRequest.METHOD_POST);
		HttpResponse httpPost = HttpHelper.httpPost(httpRequest, user);
		String stringResult = httpPost.getStringResult();
		EmployeeResponse response = mapper.fromJson(stringResult,EmployeeResponse.class);
		if(response.getErrcode()!=0){
			throw new OApiException(String.valueOf(response.getErrcode()), response.getErrmsg(), new String[]{});
		}
		return response;
		
	}
	
	
	//删除成员
	public static EmployeeResponse deleteUser(String userid) throws OApiException {
		String accessToken = AuthHelper.getAccessToken();
		HttpRequest httpRequest = new HttpRequest(null);
		String url = Env.OAPI_HOST + "/user/delete?" +
				"access_token=" + accessToken + "&userid=" + userid;
		httpRequest.setUrl(url);
		httpRequest.setMethod(HttpRequest.METHOD_GET);
		HttpResponse httpGet = HttpHelper.httpGet(httpRequest);
		String stringResult = httpGet.getStringResult();
		EmployeeResponse response = mapper.fromJson(stringResult,EmployeeResponse.class);
		if(response.getErrcode()!=0){
			throw new OApiException(String.valueOf(response.getErrcode()), response.getErrmsg(), new String[]{});
		}
		return response;
	}
	
	
	//获取成员
	public static Employee getUser(String userid){
		String accessToken = AuthHelper.getAccessToken();
		HttpRequest httpRequest = new HttpRequest(null);
		String url = Env.OAPI_HOST + "/user/get?" +
				"access_token=" + accessToken + "&userid=" + userid;
		httpRequest.setUrl(url);
		httpRequest.setMethod(HttpRequest.METHOD_GET);
		HttpResponse httpGetOrPost = HttpHelper.httpGet(httpRequest);
		String stringResult = httpGetOrPost.getStringResult();
		EmployeeResponse response = mapper.fromJson(stringResult,EmployeeResponse.class);
		if(response.getErrcode()!=0){
			throw new OApiException(String.valueOf(response.getErrcode()), response.getErrmsg(), new String[]{});
		}
		return response;
	}
	
	
	//批量删除成员
	public static EmployeeResponse batchDeleteUser( List<String> useridlist){
		String accessToken = AuthHelper.getAccessToken();
		HttpRequest httpRequest = new HttpRequest(null);
		String url = Env.OAPI_HOST + "/user/batchdelete?" +
				"access_token=" + accessToken;
		httpRequest.setUrl(url);
		httpRequest.setMethod(HttpRequest.METHOD_POST);
		
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("useridlist", useridlist);
		 HttpResponse httpGetOrPost = HttpHelper.httpPost(httpRequest, map);
		 String stringResult = httpGetOrPost.getStringResult();
			EmployeeResponse response = mapper.fromJson(stringResult,EmployeeResponse.class);
		if(response.getErrcode()!=0){
			throw new OApiException(String.valueOf(response.getErrcode()), response.getErrmsg(), new String[]{});
		}
		return response;
	}
	
	
	//获取部门成员
	public static List<Employee> getDepartmentUser(long department_id) {
		String accessToken = AuthHelper.getAccessToken();
		HttpRequest httpRequest = new HttpRequest(null);
		String url = Env.OAPI_HOST + "/user/simplelist?" +
				"access_token=" + accessToken + "&department_id=" + department_id;
		httpRequest.setUrl(url);
		httpRequest.setMethod(HttpRequest.METHOD_GET);
		HttpResponse httpGet = HttpHelper.httpGet(httpRequest);
			String stringResult = null;
				stringResult = httpGet.getStringResult();
				EmployeeListResponse response = mapper.fromJson(stringResult,EmployeeListResponse.class);
				if(response.getErrcode()!=0){
					throw new OApiException(String.valueOf(response.getErrcode()), response.getErrmsg(), new String[]{});
				}
				return response.getUserlist();
	}
	
	
	//获取部门成员（详情）
	public static List<Employee> getUserDetails(long department_id) {
		String accessToken = AuthHelper.getAccessToken();
		HttpRequest httpRequest = new HttpRequest(null);
		String url = Env.OAPI_HOST + "/user/list?" +
				"access_token=" + accessToken + "&department_id=" + department_id;
		httpRequest.setUrl(url);
		httpRequest.setMethod(HttpRequest.METHOD_GET);
		 HttpResponse httpGetOrPost = HttpHelper.httpGet(httpRequest);
			String stringResult;
				stringResult = httpGetOrPost.getStringResult();
				EmployeeListResponse response = mapper.fromJson(stringResult, EmployeeListResponse.class);
				if(response.getErrcode()!=0){
					throw new OApiException(String.valueOf(response.getErrcode()), response.getErrmsg(), new String[]{});
				}
				return response.getUserlist();
	}
	public static EmployeeResponse getUserInfo(String code) throws OApiException{
		String accessToken = AuthHelper.getAccessToken();
		HttpRequest httpRequest = new HttpRequest(null);
		String url = Env.OAPI_HOST + "/user/getuserinfo?" + "access_token=" + accessToken + "&code=" + code;
		httpRequest.setUrl(url);
		httpRequest.setMethod(HttpRequest.METHOD_GET);
		HttpResponse httpGetOrPost = HttpHelper.httpGet(httpRequest);
		String stringResult = httpGetOrPost.getStringResult();
		EmployeeResponse response = mapper.fromJson(stringResult,EmployeeResponse.class);
		if(response.getErrcode()!=0){
			throw new OApiException(String.valueOf(response.getErrcode()), response.getErrmsg(), new String[]{});
		}
		return response;
	}
	

}
